﻿using SanJ.FreeCare.ObservationDefinitionItems;
using SanJ.FreeCare.ObservationDefinitions;
using SanJ.FreeCare.ValueSets;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Domain.Services;

namespace SanJ.FreeCare.ObservationDefinitionItems
{
    public class ObservationDefinitionItemManager : IDomainService
    {
        private readonly IObservationDefinitionItemRepository _observationDefinitionItemRepository;
        public ObservationDefinitionItemManager(IObservationDefinitionItemRepository observationDefinitionItemRepository)
        {
            _observationDefinitionItemRepository = observationDefinitionItemRepository;
        }
        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="name">项目名称</param>
        /// <param name="code">项目编码</param>
        /// <param name="type">项目类型</param>
        /// <param name="description">详细描述</param>
        public async Task<ObservationDefinitionItem> CreateAsync(
           Guid observationDefinitionId,
           string? type,
           string? context,
           string? description,
           double? rangMax,
           double? rangMin,
           string? vaildCodes,
           string? inVaildCodes,
           string? effectTargets
            )
        {

            ObservationDefinitionItem observationDefinitionItem = new ObservationDefinitionItem();
            observationDefinitionItem.IsVaild = VaildType.Vaild;
            observationDefinitionItem.ObservationDefinitionId = observationDefinitionId;
            observationDefinitionItem.Context = context;
            observationDefinitionItem.Type = type;
            observationDefinitionItem.Description = description;
            observationDefinitionItem.RangMax = rangMax;
            observationDefinitionItem.RangMin = rangMin;
            observationDefinitionItem.VaildCodes = vaildCodes;
            observationDefinitionItem.InVaildCodes = inVaildCodes;
            observationDefinitionItem.EffectTargets = effectTargets;

            return await _observationDefinitionItemRepository.InsertAsync(observationDefinitionItem);
        }

        public async Task<ObservationDefinitionItem> CreateAsync(ObservationDefinitionItem odi)
        {
            ObservationDefinitionItem observationDefinitionItem = new ObservationDefinitionItem();
            if (observationDefinitionItem.IsVaild.IsNullOrEmpty())
            {
                observationDefinitionItem.IsVaild = VaildType.Vaild;
            }
            observationDefinitionItem.ObservationDefinitionId = odi.ObservationDefinitionId;
            observationDefinitionItem.Context = odi.Context;
            observationDefinitionItem.Type = odi.Type;
            observationDefinitionItem.Description = odi.Description;
            observationDefinitionItem.RangMax = odi.RangMax;
            observationDefinitionItem.RangMin = odi.RangMin;
            observationDefinitionItem.VaildCodes = odi.VaildCodes;
            observationDefinitionItem.InVaildCodes = odi.InVaildCodes;
            observationDefinitionItem.EffectTargets = odi.EffectTargets;


            return await _observationDefinitionItemRepository.InsertAsync(observationDefinitionItem);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="odi"></param>
        /// <returns></returns>
        public Task DeleteAsync(Guid id)
        {
            return _observationDefinitionItemRepository.DeleteAsync(id);
        }

        /// <summary>
        /// 直接删除
        /// </summary>
        public Task DeleteDirectAsync(Guid id)
        {
            return _observationDefinitionItemRepository.DeleteDirectAsync(p => p.Id == id);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="odi"></param>
        /// <returns></returns>
        public Task DeleteByObdIdAsync(Guid id)
        {
            return _observationDefinitionItemRepository.DeleteDirectAsync(p => p.ObservationDefinitionId == id);
        }



        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="id"></param>
        /// <param name="odi"></param>
        /// <returns></returns>
        public Task UpdateAsync(Guid id, ObservationDefinitionItem odi)
        {
            return _observationDefinitionItemRepository.UpdateAsync(odi);
        }
        /// <summary>
        /// 单查
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Task GetByIdAsync(Guid id)
        {
            return _observationDefinitionItemRepository.GetAsync(id);
        }
        /// <summary>
        /// 多查
        /// </summary>
        /// <param name="listId"></param>
        /// <returns></returns>
        public async Task<List<ObservationDefinitionItem>> GetListByObservationDefinitionIds(List<Guid> obd_Ids)
        {
            return await _observationDefinitionItemRepository.GetListByObdIdsAsync(obd_Ids);
        }

        public async Task<List<ObservationDefinitionItem>> GetListByObservationDefinitionId(Guid obdId)
        {
            return await _observationDefinitionItemRepository.GetListByObdIdAsync(obdId);
        }
    }
}
